var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
import { emptyObject, getType, toKebabCase, isEmptyObject } from "utils-lib-js";
import { ANSIParams } from "./static.js";
var ANSI = (function () {
    function ANSI() {
        var _this = this;
        this.moveTo = function (opts) {
            var _a;
            var _b = opts !== null && opts !== void 0 ? opts : {}, direct = _b.direct, position = _b.position;
            if (!direct || !position)
                return "";
            var _p = "";
            if (direct === "custom" && typeof position === "object") {
                var col = position.col, row = position.row;
                _p = "".concat(row, ";").concat(col);
            }
            else if (typeof position !== "object") {
                _p = "".concat(position);
            }
            if (_p)
                return "".concat(_p).concat((_a = ANSIParams.cursor.direct[direct]) !== null && _a !== void 0 ? _a : "");
            return "";
        };
        this.scrollTo = function (row) {
            if (!row)
                return "";
            var direct = ANSIParams.scroll.down;
            if (row < 0) {
                direct = ANSIParams.scroll.up;
            }
            return "".concat(Math.abs(row)).concat(direct);
        };
        this.getCursor = function (cursor) {
            var _a;
            if (cursor)
                return "".concat((_a = ANSIParams.cursor[cursor]) !== null && _a !== void 0 ? _a : "");
            else
                return "";
        };
        this.getColorMode = function (mode) { var _a; return "".concat((_a = ANSIParams.colorMode[mode]) !== null && _a !== void 0 ? _a : "2"); };
        this.getColorType = function (type) { var _a; return "".concat((_a = ANSIParams.colorType[type]) !== null && _a !== void 0 ? _a : "38"); };
        this.getRGB = function (options) {
            var _a = options.type, type = _a === void 0 ? "foreground" : _a, _b = options.mode, mode = _b === void 0 ? "trueColor" : _b, _c = options.red, red = _c === void 0 ? 0 : _c, _d = options.green, green = _d === void 0 ? 0 : _d, _e = options.blue, blue = _e === void 0 ? 0 : _e, _f = options.color, color = _f === void 0 ? 0 : _f;
            var cType = _this.getColorType(type), cMode = _this.getColorMode(mode);
            if (cMode === ANSIParams.colorMode.compatibility)
                return "".concat(cType, ";").concat(cMode, ";").concat(color);
            return "".concat(cType, ";").concat(cMode, ";").concat(red, ";").concat(green, ";").concat(blue);
        };
        this.getColor = function (color) {
            var _a;
            if (color)
                return "".concat((_a = ANSIParams.color[color]) !== null && _a !== void 0 ? _a : "");
            else
                return "";
        };
        this.getTextStyle = function (textStyle) {
            var _a;
            if (textStyle)
                return "".concat((_a = ANSIParams.textStyle[textStyle]) !== null && _a !== void 0 ? _a : "");
            else
                return "";
        };
        this.getANSI = function (params, isStyle) {
            if (isStyle === void 0) { isStyle = true; }
            return params ? "\u001B[".concat(params).concat(isStyle ? "m" : "") : "";
        };
        this.reset = function () { return _this.getANSI("0"); };
    }
    return ANSI;
}());
var defaultOpts = {
    reset: true,
    type: "log",
    split: true
};
var JSLog = (function (_super) {
    __extends(JSLog, _super);
    function JSLog(globalOpts) {
        var _this = _super.call(this) || this;
        _this.stylePlaceholder = "%c";
        _this.isNode = typeof process !== "undefined";
        _this.globalOpts = defaultOpts;
        _this.log = function () {
            var args = [];
            for (var _i = 0; _i < arguments.length; _i++) {
                args[_i] = arguments[_i];
            }
            if (args.length <= 0)
                return _this;
            var _a = _this.createQueue(args), logQueue = _a.logQueue, styleQueue = _a.styleQueue;
            _this.logFn(logQueue, styleQueue);
            return _this;
        };
        _this.createQueue = function (args) {
            var styleQueue = [], logQueue = [];
            var _a = _this, isNode = _a.isNode, stylePlaceholder = _a.stylePlaceholder, globalOpts = _a.globalOpts;
            args.forEach(function (it) {
                _this.checkOptions(it);
                var _a = it.text, text = _a === void 0 ? "" : _a;
                var _opts = emptyObject(__assign(__assign({}, globalOpts), it));
                var reset = _opts.reset, _b = _opts.style, style = _b === void 0 ? {} : _b;
                var hasStyle = !isEmptyObject(style);
                logQueue.push("".concat(_this.formate(_opts)).concat(hasStyle ? stylePlaceholder : "").concat(text).concat(reset ? _this.reset() : ""));
                if (hasStyle) {
                    var _style = _this.formatStyle(style);
                    _style && styleQueue.push(_style);
                }
            });
            if (isNode && globalOpts.split) {
                return { logQueue: logQueue, styleQueue: styleQueue };
            }
            return { logQueue: logQueue.join(""), styleQueue: styleQueue };
        };
        _this.checkOptions(globalOpts);
        _this.mixins(globalOpts);
        return _this;
    }
    JSLog.prototype.mixins = function (opts) {
        this.globalOpts = emptyObject(__assign(__assign({}, this.globalOpts), opts));
        return this.globalOpts;
    };
    JSLog.prototype.clear = function () {
        this.globalOpts = emptyObject(defaultOpts);
    };
    JSLog.prototype.formate = function (opts) {
        var color = opts.color, textStyle = opts.textStyle, cursor = opts.cursor, move = opts.move, scroll = opts.scroll;
        var params = this.getANSI("".concat(this.formatCursor(cursor)).concat(this.moveTo(move)).concat(this.scrollTo(scroll)), false) + this.getANSI("".concat(this.formatColor(color)).concat(this.formatText(textStyle)));
        return params;
    };
    JSLog.prototype.formatCursor = function (cursor) {
        if (typeof cursor === "string") {
            return this.getCursor(cursor);
        }
        return "";
    };
    JSLog.prototype.formatText = function (textStyle) {
        var _this = this;
        if (typeof textStyle === "string") {
            return this.getTextStyle(textStyle);
        }
        else if (typeof textStyle === "object") {
            var __temp_1 = "";
            textStyle.forEach(function (it) { return __temp_1 += _this.getTextStyle(it); });
            return __temp_1;
        }
        return "";
    };
    JSLog.prototype.formatColor = function (color) {
        var _this = this;
        if (typeof color === "string") {
            return this.getColor(color);
        }
        else if (typeof color === "object") {
            if (getType(color) === "array") {
                var __temp_2 = "";
                color.forEach(function (it) { return __temp_2 += _this.formatColor(it); });
                return __temp_2;
            }
            else {
                return this.getRGB(color);
            }
        }
        return "";
    };
    JSLog.prototype.formatStyle = function (styles) {
        var styleStr = "";
        Reflect.ownKeys(styles).forEach(function (k) {
            if (typeof k === "string")
                styleStr += "".concat(toKebabCase(k), ":").concat(styles[k], ";");
        });
        return styleStr;
    };
    JSLog.prototype.logFn = function (logQueue, styleQueue) {
        var globalOpts = this.globalOpts;
        var type = globalOpts.type;
        if (typeof logQueue === "string") {
            return console[type].apply(console, __spreadArray([logQueue], styleQueue, false));
        }
        return console[type].apply(console, __spreadArray(__spreadArray([], logQueue, false), styleQueue, false));
    };
    JSLog.prototype.checkOptions = function (opts) {
        if (opts === void 0) { opts = {}; }
        var isNode = this.isNode;
        var cursor = opts.cursor, move = opts.move, scroll = opts.scroll, style = opts.style, color = opts.color, textStyle = opts.textStyle;
        if (isNode && style)
            throw Error("node环境下无法使用style相关属性");
        if (!isNode && (cursor || move || scroll))
            throw Error("window环境下无法使用光标相关属性");
        if (!isNode && style && (color || textStyle))
            console.warn("请注意：样式可能会冲突");
    };
    return JSLog;
}(ANSI));
export { JSLog };
export default JSLog;
